package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpPositionAddDTO;
import org.jsola.hr.dto.EmpPositionBatchDTO;
import org.jsola.hr.dto.EmpPositionUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.EmpPositionQuery;
import org.jsola.hr.service.IEmpPositionService;
import org.jsola.hr.vo.EmpContractExcelVO;
import org.jsola.hr.vo.EmpPaperContractExcelVO;
import org.jsola.hr.vo.EmpPositionListVO;
import org.jsola.hr.vo.EmpPositionVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Objects;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrEmpPositionController")
@RequestMapping(HrConstants.API_PATH + "/v1/empPositions")
public class EmpPositionController implements IEmpPositionControllerAPI {

    @Autowired
    private IEmpPositionService empPositionService;

    @Override
    @PostMapping
    public Result<EmpPositionVO> save(@Valid @RequestBody EmpPositionAddDTO empPositionAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工岗位关系权限校验

        return Result.success(
                empPositionService.save(empPositionAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpPositionUpdateDTO empPositionUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工岗位关系权限校验

        int count = empPositionService.update(empPositionUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody EmpPositionBatchDTO empPositionBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<EmpPositionAddDTO> empPositionAddDTOList = empPositionBatchDTO.getEmpPositionAddDTOList();
        if (!CollectionUtils.isEmpty(empPositionAddDTOList)) {
            empPositionService.batchSave(empPositionAddDTOList,tokenUser);
        }
        // 修改
        List<EmpPositionUpdateDTO> empPositionUpdateDTOList = empPositionBatchDTO.getEmpPositionUpdateDTOList();
        if (!CollectionUtils.isEmpty(empPositionUpdateDTOList)) {
            empPositionUpdateDTOList.parallelStream().forEach(
                    empPositionUpdateDTO -> {
                        empPositionService.update(empPositionUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteEmpPositionIdList = empPositionBatchDTO.getDeleteEmpPositionIdList();
        if (!CollectionUtils.isEmpty(deleteEmpPositionIdList)) {
            empPositionService.deleteByIds(tokenUser,deleteEmpPositionIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{empPositionId}")
    public Result<Integer> delete(@PathVariable String empPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工岗位关系权限校验

        int count = empPositionService.deleteByIds(tokenUser, empPositionId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empPositionIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工岗位关系权限校验

        int count = empPositionService.deleteByIds(tokenUser, empPositionIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empPositionId}")
    public Result<EmpPositionVO> findById(@PathVariable String empPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工岗位关系权限校验

        return Result.success(
            empPositionService.selectById(empPositionId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpPositionListVO>> listAll(@Valid EmpPositionQuery empPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工岗位关系权限校验

        return Result.success(
            empPositionService.select(empPositionQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpPositionListVO>> page(@Valid EmpPositionQuery empPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empPositionService.selectPage(empPositionQuery, tokenUser.getSiteId())
        );
    }


}

